package com.guli.gulimall.product.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.CategoryEntity;
import com.guli.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    private CategoryDao categoryDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> treeList() {
        //查询所有的 三级 分类信息
        List<CategoryEntity> allList = baseMapper.selectList(null);
        //在allList中获取一级分类集合
        List<CategoryEntity> l1List = allList.stream().filter(item -> {
            return item.getParentCid().equals(0l);
        }).map(item -> {//过滤之后的结果
            //需要在这个位置找到二级分类
            //找子元素的方法 children 二级分类 包含了三级分类
            List<CategoryEntity> children = getChildren(allList, item);
            item.setChildren(children);
            return item;
        }).sorted((s1,s2)->{
            return (s1.getSort()!=null?s1.getSort():0)-(s2.getSort()!=null?s2.getSort():0);
        }).collect(Collectors.toList());
        return l1List;
    }

    @Override
    public Long[] getCatePath(Long catelogId) {
        QueryWrapper<CategoryEntity> w = new QueryWrapper<>();
        w.eq("cat_id",catelogId);
        CategoryEntity category = baseMapper.selectOne(w);
        QueryWrapper<CategoryEntity> w1 = new QueryWrapper<>();
        w1.eq("cat_id",category.getParentCid());
        CategoryEntity categorys = baseMapper.selectOne(w1);
        Long[] longs = new Long[3];
        longs[0] = categorys.getParentCid();
        longs[1] = category.getParentCid();
        longs[2] = catelogId;
        return longs;
    }

    private List<CategoryEntity> getChildren(List<CategoryEntity> allList,CategoryEntity categoryEntity) {
        List<CategoryEntity> children = allList.stream().filter(item->{
            return item.getParentCid().equals(categoryEntity.getCatId());
        }).map(item->{
            //查询三级分类
            List<CategoryEntity> children1 = getChildren(allList, item);
            item.setChildren(children1);
            return item;

        }).sorted((s1,s2)->{
            return (s1.getSort()!=null?s1.getSort():0)-(s2.getSort()!=null?s2.getSort():0);
        }).collect(Collectors.toList());
        return children;
    }
   /* @Override
    public List<CategoryEntity> treeList() {
        //1、三级分类树
        //1、先查询一级分类 查询二级
        //2、遍历所有的一级分类查询二级
        //3、遍历所有的二级分类查询三级
        QueryWrapper<CategoryEntity> w1 = new QueryWrapper<>();
        //查询一级分类
        w1.eq("parent_cid",0);
        List<CategoryEntity> l1List = baseMapper.selectList(w1);
        for (CategoryEntity l1 : l1List) {
            Long catId = l1.getCatId();
            QueryWrapper<CategoryEntity> w2 = new QueryWrapper<>();
            w2.eq("parent_cid",catId);
            List<CategoryEntity> l2List = baseMapper.selectList(w2);
            l1.setChildren(l2List);
            for (CategoryEntity l2 : l2List) {
                List<CategoryEntity> l3List = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid",l2.getCatId()));
                l2.setChildren(l3List);
            }
        }

        return l1List;
    }*/

}